import UIKit
import Flutter
import UserNotifications

@main
@objc class AppDelegate: FlutterAppDelegate {
    
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            GeneratedPluginRegistrant.register(with: self)
            UNUserNotificationCenter.current().delegate = self
            return super.application(application, didFinishLaunchingWithOptions: launchOptions)
        }
    
    // Add your UNUserNotificationCenterDelegate methods here
    override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        let userInfo = notification.request.content.userInfo
#if canImport(UmengPushSdkPlugin)
        UmengPushSdkPlugin.didReceiveUMessage(userInfo)
#else
#endif
        if notification.request.trigger is UNPushNotificationTrigger {
#if canImport(UMPush)
            UMessage.setAutoAlert(false)
            UMessage.didReceiveRemoteNotification(userInfo) // 应用处于前台时的远程推送接收 // 必须加这句代码
#else
#endif
        } else {
            // 应用处于前台时的本地推送接收
        }
        // 控制推送消息是否直接在前台显示
        completionHandler([.sound, .badge, .alert])
    }


    override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
#if canImport(UmengPushSdkPlugin)
        //应用后台或者关闭时收到消息，回调flutter层onNotificationOpen方法
        UmengPushSdkPlugin.didOpenUMessage(userInfo)
#else
#endif
        if response.notification.request.trigger is UNPushNotificationTrigger {
#if canImport(UMPush)
            UMessage.didReceiveRemoteNotification(userInfo)             // 应用处于后台时的远程推送接收 // 必须加这句代码
#else
#endif
        } else {
            // 应用处于后台时的本地推送接收
        }
        completionHandler()
    }
}
